#include "host_cmd_huge.h"
#ifndef _WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#else

#endif // _WIN32
#include "imghost_api.h"

CHostCmd_huge::CHostCmd_huge()
{

}

CHostCmd_huge::~CHostCmd_huge()
{

}

int CHostCmd_huge::AnalysisCmd(char * cmd_buf,int cmd_len)
{
	int ret = 0;

	m_byte_parser.init_data(cmd_buf, cmd_len);
	m_respond.clear();

	if(m_show_receive)
		m_byte_parser.print_byte("recv");

	try 
	{
		luacpp::call<void>(m_l, "analysis_cmd", m_byte_parser, m_respond);
	}
	catch (std::string& err)
	{
		printf("%s cmd size %d\n", err.c_str(), m_byte_parser.size());
		return -1;
	}
	
	int respond_ret = RespondDataPkt((char*)m_respond.data(), m_respond.size());
	if (m_show_send)
		m_respond.print_byte("respond");

	return ret;
}

int CHostCmd_huge::Init_host_lua(string lua_file)
{
	int data_len = 0;
	//注册字节数组打包 解析类
	luacpp::reg_cclass<CBytearr_parser>::_reg(m_l, "bytearr_parser")
		.constructor<void>()//无参构造
		.method("get_byte", &CBytearr_parser::get_byte)
		.method("get_uint16", &CBytearr_parser::get_uint16)
		.method("get_uint32", &CBytearr_parser::get_uint32)
		.method("get_uint64", &CBytearr_parser::get_uint64)
		.method("get_index", &CBytearr_parser::get_index)
		.method("pull_byte", &CBytearr_parser::pull_byte)
		.method("pull_uint16", &CBytearr_parser::pull_uint16)
		.method("pull_uint32", &CBytearr_parser::pull_uint32)
		.method("pull_uint64", &CBytearr_parser::pull_uint64)
		.method("pull_uint16_be", &CBytearr_parser::pull_uint16_be)
		.method("pull_uint32_be", &CBytearr_parser::pull_uint32_be)
		.method("pull_uint64_be", &CBytearr_parser::pull_uint64_be)
		.method("pull_string", &CBytearr_parser::pull_string)
		.method("endian_swap16", &CBytearr_parser::EndianSwap16)
		.method("endian_swap32", &CBytearr_parser::EndianSwap32);

	luacpp::reg_cclass<CByteArray>::_reg(m_l, "byte_array")
		.constructor<void>()//无参构造
		.method("clear", &CByteArray::clear)
		.method("size", &CByteArray::size)
		.method("push_byte", &CByteArray::push_byte)
		.method("push_uint16", &CByteArray::push_uint16)
		.method("push_uint32", &CByteArray::push_uint32)
		.method("push_uint64", &CByteArray::push_uint64)
		.method("push_uint16_be", &CByteArray::push_uint16_be)
		.method("push_uint32_be", &CByteArray::push_uint32_be)
		.method("push_uint64_be", &CByteArray::push_uint64_be)
		.method("push_string", &CByteArray::push_string);

	//注册功能函数
	//注册C函数

	luacpp::reg_cfun(m_l, "disk_is_init",    HM_disk_is_init);
	luacpp::reg_cfun(m_l, "disk_format_rec", HM_FormatToRecdisk);
	luacpp::reg_cfun(m_l, "stor_rec_start",  HM_RecordStart);
	luacpp::reg_cfun(m_l, "stor_rec_stop",   HM_RecorcStop);
	luacpp::reg_cfun(m_l, "stor_get_max_tm", HM_GetRecMaxTime);

	luacpp::reg_cfun(m_l, "search_rec",      HM_SearchRecfile);
	//回放控制
	luacpp::reg_cfun(m_l, "play_start",      HM_PlaybackBytime);
	luacpp::reg_cfun(m_l, "play_file_start", HM_PlaybackByFile);
	luacpp::reg_cfun(m_l, "play_stop",       HM_PlaybackStop);
	luacpp::reg_cfun(m_l, "play_seektm",     HM_PlaybackSeekTime);
	luacpp::reg_cfun(m_l, "play_setmod",     HM_PlaybackSetMode);

	bool bRet = 0;
	bRet = m_l.dofile(lua_file.c_str());
	m_lua_file = lua_file;

	return 0;
}